package cn.gson.fengboot.model.service;

import cn.gson.fengboot.model.dao.*;
import cn.gson.fengboot.model.mapper.ZcontractMapper;
import cn.gson.fengboot.model.mapper.ZinvoiceMapper;
import cn.gson.fengboot.model.mapper.ZorderMapper;
import cn.gson.fengboot.model.mapper.ZreturnedmoneylogMapper;
import cn.gson.fengboot.model.pojos.*;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class ZorderService {
    @Autowired
    ZorderMapper ddmapper;

    @Autowired
    ZorderDao dddao;

    @Autowired
    ZorderdetailsDao ddxqdao;

    @Autowired
    ZbarterDao hhdao;

    @Autowired
    ZreturnedmoneylogMapper jlmapper;

    @Autowired
    ZcontractMapper htmapper;

    @Autowired
    TfyclientFormDao khdao;
    /*
        订单弹出框查询方法
     */
    public List<Zorder> ddlist(String search,String search2,String search3){
        return ddmapper.ddlist(search,search2,search3);
    }

    /*
        订单页面查询方法
    */
    public List<Zorder> ddlist2(String search,String search2){
        return ddmapper.ddlist2(search,search2);
    }

    /*
    订单详情查询方法
    */
    public List<Zorderdetails> ddxqlist(Integer search){
        return ddmapper.ddxqlist(search,null);
    }

    /*
    订单详情页面查询方法
    */
    public List<Zorderdetails> allpro(Integer search){
        return ddmapper.allpro(search);
    }

    /*
        订单详情页面查询方法弹框
    */
    public List<Zorderdetails> allpro2(Integer search,String search2,String search3){
        return ddmapper.allpro2(search,search2,search3);
    }

    /*
        客户查询方法
    */
    public List<TfyclientForm> kehulist(Integer search){
        return ddmapper.kehulist(search);
    }

    /*
        合同xq订单查询方法
    */
    public List<Zorder> ddlistheton(Integer search){
        return ddmapper.ddlistheton(search);
    }

    //根据订单id查询订单
    public List<Zorder> ddlisthh(@Param("search") Integer search){
        return ddmapper.ddlisthh(search);
    };
    /*
    新增订单
    */
    public void add(Zorder dd){
        System.out.println("我进来了");
        //新增订单表
        Zorder save = dddao.save(dd);
        //新增采购详情表
        dd.getZorderdetailsByOrderId().forEach(pur->{
            pur.setOrderId(save);
        });

        if(dd.getBarterId().getBarterId()!=-1 &&dd.getOrderId()==0){
            Zbarter zbarter = hhdao.findById(dd.getBarterId().getBarterId()).get();
            zbarter.setBarterState("待发货");
            zbarter.setBarterOrderNo(save);
            hhdao.save(zbarter);
        }
        ddxqdao.saveAll(dd.getZorderdetailsByOrderId());

        //根据订单数量判断是否是重复购买的客户
        Integer dd1 = htmapper.dd(dd.getClientId().getClientId());
        if(dd1>=1){
            TfyclientForm tfyclientForm = khdao.findById(dd.getClientId().getClientId()).get();
            tfyclientForm.setClientKhzt("重复购买");
            khdao.save(tfyclientForm);
        }

    }

    public Integer sc(Zorder dd){
        Integer hk = jlmapper.hk(dd.getOrderId());
        Integer jf = jlmapper.jf(dd.getOrderId());
        System.out.println("B"+hk);
        System.out.println("a"+jf);
        if(hk==0&&jf==0){
            dddao.delete(dd);
            return 0;
        }else {
            return 1;
        }
    }

    //<!--谭方圆=>客户合并的订单查询-->
    public List<Zorder> tfyddlists(Integer id1, Integer id2){
        return ddmapper.tfyddlists(id1,id2);
    }
}
